package com.mwh.yuantu.core;

//import io.swagger.annotations.ApiModel;
//import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 统一API响应结果封装
 */
@Schema(name = "Result", title = "接口通用返回结果结构")
public class Result implements Serializable {

	private static final long serialVersionUID = 1L;
	@Schema(title = "编码")
	private Integer code;
//	@ApiModelProperty(value = "提示", name = "message", dataType = "string", example = "成功")
	@Schema(title = "提示")
	private String message = "成功";

//	@ApiModelProperty(value = "内容", name = "data", dataType = "t")
	@Schema(title = "内容")
	private Map<String, Object> data = new HashMap<String, Object>();

	public Result() {

	}

	public Result success(String message) {
		this.message = message;
		this.code = ResultCode.SUCCESS.code();
		return this;
	}

	public static Result ok() {
		Result r = new Result();
		r.setCode(ResultCode.SUCCESS.code());
		r.setMessage("成功");
		return r;
	}

	public static Result ok(Map<String, Object> data) {
		Result r = new Result();
		r.setMessage("成功");
		r.setCode(ResultCode.SUCCESS.code());
		r.setData(data);
		return r;
	}

	public static Result ok(Map<String, Object> data, String msg) {
		Result r = new Result();
		r.setData(data);
		r.setMessage(msg);
		return r;
	}

	// 链式操作返回信息
	public Result add(String key, Object value) {
		this.getData().put(key, value);
		return this;
	}

	public static Result error(String msg) {
		return error(ResultCode.FAIL.code(), msg);
	}

	public static Result confirm(String msg) {
		return error(ResultCode.CONFIRM.code(), msg);
	}

	public static Result error(int code, String msg) {
		Result r = new Result();
		r.setCode(code);
		r.setMessage(msg);
		return r;
	}

	public Result(int code, String message) {
		this.code = code;
		this.message = message;
	}

	public static Result noauth(String msg) {
		return error(ResultCode.PERMISSION_ERROR.code(), msg);
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public static Boolean isError(Result r) {
		return !isSuccess(r);
	}

	public static Boolean isSuccess(Result r) {
		return ResultCode.SUCCESS.code() == r.getCode();
	}

	@Override
	public String toString() {
		return "Result{" +
				"code=" + code +
				", message='" + message + '\'' +
				", data=" + data +
				'}';
	}
}
